package com.ywyself.controller.admin;

import com.jfinal.core.ActionKey;
import com.ywyself.controller.base.YsController;
import com.ywyself.ext.kit.YsKit;
import com.ywyself.ext.utils.YsStringUtils;
import com.ywyself.service.SpiderService;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SpiderController extends YsController {

    @ActionKey(ADMIN_ROOT + "/spider")
    public void index() {
        forwardAction(ADMIN_ROOT + "/spider/task/add");
    }

    /**
     * 查看正在进行的任务
     */
    @ActionKey(ADMIN_ROOT + "/spider/task")
    public void task() {
        setPageTitle("任务列表");
        List<Map<String, Object>> list = SpiderService.EXC.listRunningThread();
        if (list == null) {
            setAttr("list", new ArrayList<>());
        } else {
            setAttr("list", list);
        }
        setAttr("tab", "spider_task");
        render(BACK_STAGE + "/spider/task.html");
    }

    /**
     * 添加爬取任务 - 界面
     */
    @ActionKey(ADMIN_ROOT + "/spider/task/add")
    public void taskAdd() {
        setPageTitle("添加任务");
        setAttr("tab", "spider_task_add");
        render(BACK_STAGE + "/spider/task_add.html");
    }

    /**
     * 添加爬取任务界面 - 保存/执行
     */
    @ActionKey(ADMIN_ROOT + "/spider/task/save")
    public void taskSave() {
        // 类文件名
        String class_name = getPara("class_name");
        // 网站地址
        String site_url = getPara("site_url");
        // 目录地址
        String cate_url = getPara("cate_url");
        // 文件夹名
        String save_dir = getPara("save_dir");
        // 从哪个章节开始下载
        String startChapter = getPara("start_chapter");
        YsKit ret = SpiderService.EXC.addTask(class_name, site_url, cate_url, save_dir, startChapter);
        if (ret.isOk()) {
            renderJson(ret.code("102").set("url", ADMIN_ROOT + "/spider/task"));
        } else {
            renderJson(ret);
        }
    }

    /**
     * 合并文件 - 界面
     */
    @ActionKey(ADMIN_ROOT + "/spider/file/merge")
    public void fileMerge() {
        List<String> list = SpiderService.EXC.listFold();
        setAttr("list", list);
        setPageTitle("合并单文件");
        setAttr("tab", "spider_file_merge");
        render(BACK_STAGE + "/spider/file_merge.html");
    }

    /**
     * 合并文件 - 保存/执行
     */
    @ActionKey(ADMIN_ROOT + "/spider/file/merge/save")
    public void fileMergeSave() {
        String foldName = getPara("fold_name");
        YsKit ret = SpiderService.EXC.mergeFiles(foldName);
        renderJson(ret);
    }

    /**
     * 文件下载，下载合并文件
     */
    @ActionKey(ADMIN_ROOT + "/spider/file/merge/download")
    public void fileMergeDownload() {
        String foldName = getPara();
        if (YsStringUtils.isBlank(foldName)) {
            renderHtml("<center><h2>文件夹名不能为空</h2></center>");
            return;
        }
        try {
            foldName = URLDecoder.decode(foldName.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.getLocalizedMessage();
            renderHtml("<center><h2>" + e.getLocalizedMessage() + "</h2></center>");
        }
        File ret = SpiderService.EXC.download(foldName);
        if (ret == null) {
            renderHtml("<center><h2>没有文件</h2></center>");
        } else {
            renderFile(ret);
        }
    }
}
